capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local log "";
local do "";
local crime "";
local data "";
local output "";
local zip "" ;
local earn "";
local parearn "";
local paroth "";

/* 
Main Prep 3, period-specific denominators (denom)
*/


local exp "prworard";
local range "1997(3)1997" ; 
local firstyr 1997 ;	
local lastyr 2019 ;		
local lastearnyr 2019 ;

/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

local first 1996 ;
local last 2014 ;
local per_range "4 5 20" ; 
local thresh 0.8 ; 	
include "`do'\crime_statecntyselect.do" ;

use "`crime'\prep2\crime_prwora_remerge.dta", clear;

/* Diagnosis */
tab n_diag1cat, m;
gen byte n_diag1_infec=(n_diag1cat==1);
gen byte n_diag1_neo=(n_diag1cat==2);
gen byte n_diag1_endo=(n_diag1cat==3);
gen byte n_diag1_blood=(n_diag1cat==4);
gen byte n_diag1_mental=(n_diag1cat==5);
gen byte n_diag1_nerv=(n_diag1cat==6);
gen byte n_diag1_sense=(n_diag1cat==7);
gen byte n_diag1_circ=(n_diag1cat==8);
gen byte n_diag1_resp=(n_diag1cat==9);
gen byte n_diag1_dig=(n_diag1cat==10);
gen byte n_diag1_gu=(n_diag1cat==11);
gen byte n_diag1_preg=(n_diag1cat==12);
gen byte n_diag1_skin=(n_diag1cat==13);
gen byte n_diag1_musc=(n_diag1cat==14);
gen byte n_diag1_cong=(n_diag1cat==15);
gen byte n_diag1_nat=(n_diag1cat==16);
gen byte n_diag1_ill=(n_diag1cat==17);
gen byte n_diag1_inj=(n_diag1cat==18);
gen byte n_diag1_none=(n_diag1cat==.);
replace n_diag1cat = 999 if n_diag1cat == . ;

/* Family structure */
tab toa, m;
gen byte n_fam_singmom=	(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=	(toa=="DC" | toa=="BC");
tab n_fam_singmom, m;
tab n_fam_nopar, m;

/* Male */
tab n_male, m;

/* Severity */
tab dibmdr, m;
gen byte n_dibmdr_miss=(dibmdr=="");
gen byte n_dibmdr_mip=(dibmdr=="3");
gen byte n_dibmdr_mine=(dibmdr=="5" | dibmdr=="7");
gen byte n_dibmdr_mie=(!n_dibmdr_miss & !n_dibmdr_mip & !n_dibmdr_mine);
tab n_dibmdr_mie, m;
tab n_dibmdr_mip, m;
tab n_dibmdr_mine, m;

/* Age at entry */
tab n_firstage, m;
replace n_firstage = 999 if n_firstage == . ;
tab n_firstage, m ;

/* Parent earnings and household income */
local earncut = 5000 ;

order n_hhearns1996_*  n_combpay_* n_hhmbayr1996_* n_hhcombpay1996_* n_sibpayall_* ;
forval yr=1990(1)2012 { ;
	gen n_inchh1996_`yr' = n_hhearns1996_`yr' + n_combpay_`yr' + n_hhmbayr1996_`yr' + n_hhcombpay1996_`yr' + n_sibpayall_`yr' if n_hhearns1996_`yr' != . ;
} ;
forval yr=1990(1)2012 { ;
	gen n_inchh_`yr' = n_hhearns_`yr' + n_combpay_`yr' + n_hhmbayr_`yr' + n_hhcombpay_`yr' + n_sibpayall_`yr' if n_hhearns_`yr' != . ;
} ;
summ n_inchh* ;

egen n_earnhh9702=rowmean(n_hhearns_1997-n_hhearns_2002) ;
egen n_earnhh9712=rowmean(n_hhearns_1997-n_hhearns_2012) ;
egen n_earnhhpre9095=rowmean(n_hhearns_1990-n_hhearns_1995) ;

egen n_inchh9702=rowmean(n_inchh_1997-n_inchh_2002) ;
egen n_inchh9712=rowmean(n_inchh_1997-n_inchh_2012) ;
egen n_inchhpre9095=rowmean(n_inchh_1990-n_inchh_1995) ;

forval yr=1990(1)2012 { ;
	gen byte temp`yr' = (n_hhearns_`yr' > `earncut') ;
	replace temp`yr' = . if n_hhearns_`yr' == . ;
} ;
egen n_emphhpost9702=rowmean(temp1997-temp2002) ;
egen n_emphhpost9712=rowmean(temp1997-temp2012) ;
egen n_emphhpre9095=rowmean(temp1990-temp1995) ;
drop temp* ;

xtile n_earnhhbins = n_earnhhpre9095, nq(10) ;
tab n_earnhhbins, m ;
replace n_earnhhbins = 99 if n_earnhhbins == . ;
tab n_earnhhbins, m ;

/* First stage variables */
gen byte n_age18unfav=(age18_final_con==0);
egen n_onssi1922 = rowmean(n_onssi_1997-n_onssi_2000) ;

gen yob = year(dob) ;
summ yob ;
local yob_min = r(min) ;
local yob_max = r(max) ;
gen mob = month(dob) ;

forval yr = 1995(1)2003 { ;
	forval mo = 1(1)12 { ;
		gen byte n_onssimo_`yr'_`mo' = (combpmt_`yr'_`mo' > 0 & combpmt_`yr'_`mo' !=.) if combpmt_`yr'_`mo' !=. ;
	} ;
} ;

gen n_onssi1922_exact = . ;
forval yob = `yob_min'(1)`yob_max' { ;
	forval mob =1(1)12 { ;
		egen temp_`yob'_`mob' = rowmean(n_onssimo_`=`yob'+19'_`=`mob''-n_onssimo_`=`yob'+22'_`=`mob'') if yob == `yob' & mob == `mob' ;
		replace n_onssi1922_exact = temp_`yob'_`mob' if yob == `yob' & mob == `mob' ;
	} ;
} ;

gen n_onssi20 = . ;
forval yob = `yob_min'(1)`yob_max' { ;
	forval mob = 1(1)12 { ;
		replace n_onssi20 = n_onssimo_`=`yob'+20'_`mob' if yob == `yob' & mob == `mob' ;
	} ;
} ;

drop yob mob temp* ;
summ n_age18unfav n_onssi1922 n_onssi1922_exact n_onssi20 ;

/* Mortality */
gen numi_dob_mo = substr(numi_dob,1,2) ;
	replace numi_dob_mo = "" if numi_dob_mo == "00" | numi_dob_mo == "XX" ;
gen numi_dob_dd = substr(numi_dob,3,2) ;
	replace numi_dob_dd = "" if numi_dob_dd == "00" | numi_dob_dd == "XX" ;
gen numi_dob_yy = substr(numi_dob,5,4) ;
destring numi_dob_mo, replace ;
destring numi_dob_dd, replace ;
destring numi_dob_yy, replace ;
drop numi_dob ;
gen numi_dob = mdy(numi_dob_mo, numi_dob_dd, numi_dob_yy) ;

forval i = 1(1)3 { ;
	gen numi_dod`i'_mo = substr(numi_dod`i',1,2) ;
	gen numi_dod`i'_dd = substr(numi_dod`i',3,2) ;
		replace numi_dod`i'_dd = "" if numi_dod`i'_dd == "00" | numi_dod`i'_dd == "XX" ;
	gen numi_dod`i'_yy = substr(numi_dod`i',5,4) ;
	destring numi_dod`i'_mo, replace ;
	destring numi_dod`i'_dd, replace ;
	destring numi_dod`i'_yy, replace ;
	drop numi_dod`i' ;
	gen numi_dod`i' = mdy(numi_dod`i'_mo, numi_dod`i'_dd, numi_dod`i'_yy) ;
} ;
gen numi_dod = numi_dod1 ;
replace numi_dod = numi_dod2 if numi_dod == . & numi_dod2 != . ;
replace numi_dod = numi_dod3 if numi_dod == . & numi_dod3 != . ;
drop numi_dod1* numi_dod2* numi_dod3* ;
gen numi_yod = year(numi_dod) ;
gen byte numi_dthpost = (numi_yod > 1996 & numi_yod !=.) ;
tab numi_dthpost, m ;


/* ********************************* */
/* **** CREATE RUNNING VARIABLE **** */
/* ********************************* */
gen dobdd_stata	=mdy(month(dob),day(dob),year(dob)+18); 
gen dobdd_run	=dobdd_stata-mdy(08,22,1996);

gen dobwk_stata	=wofd(mdy(month(dob),day(dob),year(dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));

gen dobmo_stata	=ym(year(dob)+18,month(dob)) ;
gen dobmo_run	=dobmo_stata-mofd(mdy(08,22,1996)) ;


/* ************************** */
/* **** SAMPLE SELECTION **** */
/* ************************** */

keep if year(first_pay_dte)<1996 ;
count;


/* ********************************************************************** */
/* **** CREATE CRIME VARIABLES USING DIFFERENT GEO/YEAR DENOMINATORS **** */
/* ********************************************************************** */

rename pdscc pdscc_full ;
gen pdscc = substr(pdscc_full,1,5) ;
gen temp_st = substr(pdscc_full,1,2) ;
tab temp_st, m ;
drop temp_st pdscc_full ;

merge m:1 pdscc using `cnty_year', keepusing(keep_* cnty_* flag*) ;
drop if _merge == 2 ;
drop _merge ;
foreach var of varlist keep* { ;
	replace `var' = 0 if `var' == . ;
} ;

summ keep* ;


/* ******************************** */
/* **** DEFINE CRIME VARIABLES **** */
/* ******************************** */

local char_cond 	2 ;
local drug_cond 	3 ;
local traf_cond     4 ;
local fel_cond		5 ;
local genr_cond		6 ;
local nogn_cond  	7 ;
local prop_cond 	8 ;
local pord_cond     9 ;
local viol_cond 	10 ;
local wknd_cond		11 ;
local fcvn_cond		12 ;
local acvn_cond     13 ;
local inca_cond 	14 ;
local par_cond 		15 ;
local prob_cond 	16 ;

local dgsl_cond 1 ;
local dgot_cond 2 ;
local dgps_cond 3 ;
local polq_cond	4 ;
local poot_cond	5 ;
local povc_cond	6 ;
local povl_cond	7 ;
local powp_cond	8 ;
local prbg_cond 9 ;
local prfd_cond 10 ;
local prtf_cond 11 ;
local vrob_cond 12 ;
local vlct_cond 13 ;
local vlpa_cond 14 ;
local vlpr_cond 15 ;
local hinc_cond 16 ;

local vcch_cond 1 ;
local vcgb_cond 2 ;
local vcpd_cond 3 ;
local vcpp_cond 4 ;
local vcps_cond 5 ;

local nias_cond 1 ; 
local nide_cond 2 ; 
local nidc_cond 3 ; 
local nidp_cond 4 ; 
local nidu_cond 5 ; 
local nilq_cond 6 ; 
local niob_cond 7 ; 
local nisx_cond 8 ; 
local nitf_cond 9 ; 
local nitp_cond 10 ; 
local niwp_cond 11 ;	

local any_abbr		"any" ;
local char_abbr 	"2c" ;
local drug_abbr 	"3cd" ;
local traf_abbr 	"4ct" ;
local fel_abbr  	"5cf" ;
local genr_abbr 	"6cg" ;
local nogn_abbr     "7cn" ;
local prop_abbr 	"8cp" ;
local pord_abbr     "9co" ;
local viol_abbr 	"10cv" ;
local wknd_abbr 	"11cw" ;
local fcvn_abbr 	"12fc" ;
local acvn_abbr     "13fm" ;
local inca_abbr		"14ie" ;	
local par_abbr		"15pa" ;
local prob_abbr		"16pr" ;
local ipp_abbr		"ipp" ;
local jail_abbr		"jail" ;

foreach stub in 
	dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc  
	nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp
	vcch vcgb vcpd vcpp vcps
	{ ;
		local `stub'_abbr "``stub'_cond'`stub'" ;
	} ;

local type_list_main "char genr nogn drug viol prop pord fel fcvn acvn inca" ; 
local type_list_bk "dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc" ;
local type_list_nobk "nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp" ;
local type_list_vice "vcch vcgb vcpd vcpp vcps" ;
local type_list "`type_list_main' `type_list_bk' `type_list_nobk' `type_list_vice' " ;
local type_list_key "char genr nogn drug viol prop dgsl povc prbg prfd prtf vrob vcps" ;
local type_list_keysing "drug viol prop dgsl povc prbg prfd prtf vrob vcps" ;

foreach stub in `type_list_main' { ;
	local `stub'_event "event" ;
} ;
foreach stub in `type_list_bk' { ;
	local `stub'_event "bk_event" ;
} ;
foreach stub in `type_list_nobk' { ;
	local `stub'_event "nobk_event" ;
} ;
foreach stub in `type_list_vice' { ;
	local `stub'_event "vice_event" ;
} ;


rename indiv_match temp1 ;

forval i=1(1)9 { ;
	rename event_date0`i' event_date`i' ;
	rename event0`i' event`i' ;
	rename day_of_week0`i' day_of_week`i' ;
	rename event_state0`i' event_state`i' ;
} ;

foreach class in bk nobk vice { ;
	forval i=1(1)9 { ;
		rename `class'_event_date0`i' 	`class'_event_date`i' ;
		rename `class'_event0`i' 		`class'_event`i' ;
		rename `class'_event_state0`i' 	`class'_event_state`i' ;
	} ;
} ;

gen byte temp2 = (event_date1 != .) ;

forval i = 1(1)2 { ;
		gen n_crime`i'any_post = temp`i' if 	keep_`firstyr'post_any == 1 ;
} ;
drop temp* ;

foreach class in event bk_event nobk_event vice_event { ;
	forval i=1(1)20 { ;
		recast double `class'`i' ;
		tostring(`class'`i'), replace format(%18.0g) ;
		forval j = 3(1)9 { ;
			replace `class'`i' = subinstr(`class'`i',"`j'","2",.) ;
		} ;
	} ;
} ;

forval yr = 1997(1)2019 { ;
	gen days_in_ipp`yr' = days_in_prison`yr' + days_in_parole`yr' + days_in_probation`yr' ;
} ;


/* **** CRIME AND EMPLOYMENT JOINT OUTCOME **** */
local empcut = 0.5 ;
forval yr=`firstyr'(1)`lastearnyr' { ;
	rename earn`yr' n_yrkidearn`yr' ;
	forval amt = 5(10)15 { ;
		gen byte n_yrkidemp`amt'_`yr' = (n_yrkidearn`yr' > `amt' * 1000) ;
		replace  n_yrkidemp`amt'_`yr' = . if n_yrkidearn`yr' == . ;
	} ;
} ;

order n_yrkidearn* ;
forval amt = 5(10)15 { ;
	order n_yrkidemp`amt'* ;
} ;

/* Kid earnings and employment */
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			egen avg_kidearn_`start'_`end' = rowmean(n_yrkidearn`start'-n_yrkidearn`end') ;
		} ;
	} ;

	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			forval amt = 5(10)15 { ;
				egen avg_kidemp`amt'_`start'_`end' = rowmean(n_yrkidemp`amt'_`start'-n_yrkidemp`amt'_`end') ;
			} ;
		} ;
	} ;

/* On SSI */
foreach var in onssi combpay { ;
	forval yr=2015(1)2019 { ;
		gen double n_`var'_`yr' = n_`var'_2014 ;		
	} ;
	order n_`var'_* ;

	egen avg_`var'_post = rowmean(n_`var'_`firstyr'-n_`var'_2019) ;

	forval start = `range' { ;
	foreach end in `list_`start'' { ;
		egen avg_`var'_`start'_`end' = rowmean(n_`var'_`start'-n_`var'_`end') ;
	} ;
	} ;
} ;

local full_hinc "hprison" ;
local full_inca "prison" ;
local full_par  "parole" ;
local full_prob "probation" ;
local full_ipp 	"ipp" ;
local full_jail "jail" ;

foreach abbr in hinc { ;
	forval yr = 1997(1)2019 { ;
		gen byte any_`abbr'`yr' = (days_in_`full_`abbr''`yr' > 0) ;
		replace  any_`abbr'`yr' = . if days_in_`full_`abbr''`yr' == . ;
	} ;
} ;



/* *********** PERIOD-SPECIFIC DENOMINATORS ******* */
local num_per = 4 ;
local begin_1 = 1997 ;
local end_1   = 2001 ;
local begin_2 = 2002 ;
local end_2   = 2006 ;
local begin_3 = 2007 ;
local end_3   = 2012 ;
local begin_4 = 2013 ;
local end_4   = 2017 ;

forval j=1(1)`num_per' { ;

	/* Any */
			gen 		n_`j'crime2any_`begin_`j''_`end_`j'' = 0 	
				if (keep_`begin_`j''_`end_`j''_any == 1) ;
			forval i=1(1)20 { ;
				replace n_`j'crime2any_`begin_`j''_`end_`j'' = 1 	
				if (keep_`begin_`j''_`end_`j''_any == 1) & year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' ;
				} ;
			
			gen 		n_`j'crime2anyt_`begin_`j''_`end_`j''= 0 
					if (keep_`begin_`j''_`end_`j''_any == 1) ;
			forval i=1(1)20 { ;
				replace n_`j'crime2anyt_`begin_`j''_`end_`j'' = n_`j'crime2anyt_`begin_`j''_`end_`j'' + 1 
					if (keep_`begin_`j''_`end_`j''_any == 1) & year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' ;
				} ;
				
	/* Specific type */		
	foreach type in `type_list' { ;

			gen 		n_`j'crime2`type'_`begin_`j''_`end_`j'' = 0 	
				if (keep_`begin_`j''_`end_`j''_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_`j'crime2`type'_`begin_`j''_`end_`j'' = 1 	
				if (keep_`begin_`j''_`end_`j''_``type'_abbr' == 1) 
				& year(``type'_event'_date`i' ) >= `begin_`j'' & year(``type'_event'_date`i' ) <= `end_`j'' 
				& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
			
			gen 		n_`j'crime2`type't_`begin_`j''_`end_`j'' = 0 
					if (keep_`begin_`j''_`end_`j''_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_`j'crime2`type't_`begin_`j''_`end_`j'' = n_`j'crime2`type't_`begin_`j''_`end_`j'' + 1 
					if (keep_`begin_`j''_`end_`j''_``type'_abbr' == 1) 
					& year(``type'_event'_date`i' ) >= `begin_`j'' & year(``type'_event'_date`i' ) <= `end_`j'' 
					& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;		
				
	} ;	/* end type */
	
	/* IPP */
	gen 		n_`j'crime2ipp_`begin_`j''_`end_`j'' = 0 	
			if (keep_`begin_`j''_`end_`j''_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2ipp_`begin_`j''_`end_`j'' = 1 	
			if (keep_`begin_`j''_`end_`j''_ipp == 1 )
			& year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' 
			& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
		
	/* Any incarceration */
	foreach type in hinc { ;
		egen 	n_`j'crime2a`type'_`begin_`j''_`end_`j'' = rowmean(any_`type'`begin_`j''-any_`type'`end_`j'') if keep_`begin_`j''_`end_`j''_``type'_abbr' == 1 ;
		
		egen 	n_`j'crime2c`type'_`begin_`j''_`end_`j'' = rowmax(any_`type'`begin_`j''-any_`type'`end_`j'')  if keep_`begin_`j''_`end_`j''_``type'_abbr' == 1 ;
	} ;
		
	/* Crime and employment interaction */	
	foreach var in onssi combpay { ;
		egen avg_`j'`var'_`begin_`j''_`end_`j'' = rowmean(n_`var'_`begin_`j''-n_`var'_`end_`j'')  ;
	} ;
	egen avg_`j'kidearn_`begin_`j''_`end_`j'' = rowmean(n_yrkidearn`begin_`j''-n_yrkidearn`end_`j'') 		;
	forval amt = 5(10)15 { ;
		egen avg_`j'kidemp`amt'_`begin_`j''_`end_`j''  = rowmean(n_yrkidemp`amt'_`begin_`j''-n_yrkidemp`amt'_`end_`j'')  ;
	} ;
			
} ; /* end j */

/* reestimate period effects for same sample */
foreach type in char genr ahinc { ;
	gen byte samp_nm_`type' = 
		(n_1crime2`type'_`begin_1'_`end_1' != . & n_2crime2`type'_`begin_2'_`end_2' != . & n_3crime2`type'_`begin_3'_`end_3' != . & n_4crime2`type'_`begin_4'_`end_4' != .) ;
	
	forval j=1(1)`num_per' { ;
		gen n_`j'crime2`type'nm_`begin_`j''_`end_`j''  = n_`j'crime2`type'_`begin_`j''_`end_`j''  if samp_nm_`type' == 1 ;
		
		if "`type'" != "ahinc" { ;
			gen n_`j'crime2`type'tnm_`begin_`j''_`end_`j'' = n_`j'crime2`type't_`begin_`j''_`end_`j'' if samp_nm_`type' == 1 ;
		} ;
	} ;
} ;


drop temp* ;


summ n_1crime* n_2crime* n_3crime* n_4crime* n_yr* n_crime* avg_* ;

gen byte n_post=(dobdd_run>0);
tab n_post, m;

drop 
	first_name fssr_middlen last_name from_prwora from_name idu middle_name
	combpmt*
	n_combpay_* n_sibpayall_* 
	n_hhearns1996_* n_hhmbayr1996_* n_hhcombpay1996_* n_inchh1996_* 
	n_hhearns_* n_hhmbayr_* n_hhcombpay_* n_inchh_* earn*
	days_in_* event* day_of_week* 
	keep_* i ;

save "`crime'\prep2\crime_prwora_prepforreg_denom.dta", replace ;

capture log close;
